﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using E2_WEBSITE.Code;
using System.Data.Common;


namespace Code.ProductType
{
    public class ProductTypeDataAccess
    {
        protected DataProvider m_DataProvider = DataProvider.GlobalDataProvider;
        public List<ProductTypeInfo> GetListProductType()
        {
            List<ProductTypeInfo> lst = new List<ProductTypeInfo>();
            string sSql = "SELECT * FROM LOAI_SAN_PHAM";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            
            if(dt != null )
            {
                int iCount = dt.Rows.Count;
                for(int i = 0 ; i < iCount ;i++)
                {
                    int iID = Int32.Parse(dt.Rows[i][0].ToString());
                    string sName = dt.Rows[i][1].ToString();
                    ProductTypeInfo pInfo = new ProductTypeInfo();
                    pInfo.ID = iID;
                    pInfo.Name = sName;
                    lst.Add(pInfo);
                }
            }     
            return lst;
        }

        public List<string> GetListProductTypeName()
        {
            List<string> lst = new List<string>();
            string sSql = "SELECT TENLOAISANPHAM FROM LOAI_SAN_PHAM";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);

            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {                 
                    string sName = dt.Rows[i][0].ToString();                   
                    lst.Add(sName);
                }
            }
            return lst;
        }

        public int GetProductTypeIDByName(string sName)
        {
            int iID = -1;

            string sSql = "Select ID from LOAI_SAN_PHAM where TENLOAISANPHAM = '" + sName + "'";

            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if(dt != null)
            {
                iID = int.Parse(dt.Rows[0][0].ToString());
            }
            return iID;
        }
    }

}
